<?php

namespace App\Http\Controllers\Admin;

use App\Models\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Goods;
use App\Models\Order;
use App\Models\Activity;

class HomeController extends Controller
{

    public function index()
    {
        $goodsNum = Goods::count();
        $orderNum = Order::count();
        $userNum = User::count();
        $activityNum = Activity::count();
        $newOrder = Order::orderBy('add_time','desc')->get()->take(7);

        //根据状态统计:1:待支付; 2:待发货；3:已发货；4:已完成；5:已取消
        for($i=1;$i<=5;$i++){
            $orderStatus[$i]=Order::where('order_status',$i)->count();
        }

        //获取近12个月的每月数据
        $nowTimeY = date('Y');
        $nowTimeM = date('m');
        $orderByMonth=array();

        if($nowTimeM<10){
            $nowTimeM = "0".$nowTimeM;
        }
        //$nowTime = $nowTimeY.$nowTimeM;
        for($i=intval($nowTimeM);$i>=1;$i--){
            if($i<10){
                $i = "0".$i;            ;
            }
            $orderByMonth[$nowTimeY.$i] = Order::whereRaw("DATE_FORMAT(add_time,'%Y%m')=".$nowTimeY.$i)->count();
        }
        $count = count($orderByMonth);
        if($count<12){
            $oldTimeY = $nowTimeY-1;
            for($j=12;$j>=$count;$j--){
                //dd($j);
                if($j<10){
                    $j = "0".$j;            ;
                }
                $orderByMonth[$oldTimeY.$j] = Order::whereRaw("DATE_FORMAT(add_time,'%Y%m')=".$oldTimeY.$j)->count();
            }
        }

        return view('admin.home',compact(['goodsNum','orderNum','userNum','activityNum','newOrder','orderStatus','orderByMonth']));
    }


}
